<?php
class Mainmenu extends Controller {   
    
    private $imgdata = array(
    'dir' => array(
    'original' => 'images/uploads/original/',
    'thumb' => 'images/uploads/thumb/',
    'large'=>'images/uploads/large/'
    ),
    'total' => 0,
    'images' => array(),
    'error' => ''
    );
        
    public function index() {
        $iteneriesbody = $this->uri->segment(5);
        $this->load->model('mainmenu_model');
        $this->load->library("pagination");
        $config = array();
        $config["base_url"] = base_url() ."secu/mainmenu/index/";
        $config["total_rows"] = $this->mainmenu_model->list_mainmenu_count();
        $config["per_page"] = 30;
        $config["uri_segment"] = 4;
        $this->pagination->initialize($config);
        $page = ($this->uri->segment(4)) ? $this->uri->segment(4) : 0;
        $data["partnerlang"]=$this->config->item("partnerlang");
        $data["mainmenu_arr"] = $this->mainmenu_model->list_mainmenu($config["per_page"], $page, $iteneriesbody);
        $data["pagination"] = $this->pagination->create_links();
        //$data["mainmenu_lang"] = $this->mainmenu_model->get_mainmenu_lang();
        $this->load->library('breadcrumb');
        $this->breadcrumb->seperator='<div class="breadcrumb_divider"></div>';
        $this->breadcrumb->addCrumb('Mainmenu',base_url()."secu/mainmenu/");
        $data['breadcrumb'] = $this->breadcrumb->makeBread();  
        /*if (!empty($iteneriesbody)){
            $this->template->load('admintemplate','mainmenu/mainmenulist_iteneries',$data);
        }else{*/
            $this->template->load('admintemplate','mainmenu/mainmenulist',$data);
        //}

    }
    
    public function managemainmenu(){
        $partnerlang=$this->config->item('partnerlang');
        $mainmenu_id=  $this->uri->segment(5);
        $customlinks_id=  $this->uri->segment(6);
        
        $dashboard_customlinks_page_id=  $this->uri->segment(4);
        
        if (trim(strtolower($dashboard_customlinks_page_id))=='itineraries'){   //used to check whether the hit has come from the dashboard itineraries or through normal main munu links
            $customlinks_page_id=$mainmenu_id;            
            $mainmenu_id='';
        }
        $data['customlinks_id_reload']=$customlinks_id;   //used when the user selects a customlinks page type to check whether any data exist for it
        
        $this->load->model("mainmenu_model");
        if(!empty($mainmenu_id)){            
            $mainmenudetails=  $this->mainmenu_model->get_mainmenu_details_by_id($mainmenu_id);
            if($mainmenudetails){
                foreach ($partnerlang as $langkey => $langval) {
                    $tmptitle=  $this->mainmenu_model->get_mainmenu_details_lang_by_id($mainmenudetails[0]['title'], $langkey);
                    $tmpshortdesc=  $this->mainmenu_model->get_mainmenu_details_lang_by_id($mainmenudetails[0]['shortdesc'], $langkey);
                    $tmpdesc=  $this->mainmenu_model->get_mainmenu_details_lang_by_id($mainmenudetails[0]['desc'], $langkey);
                    $mainmenudetails[0]["title".$langkey]=$tmptitle[0]['lvalue'];
                    $mainmenudetails[0]["shortdesc".$langkey]=$tmpshortdesc[0]['lvalue'];
                    $mainmenudetails[0]["desc".$langkey]=$tmpdesc[0]['lvalue'];
                }
                /*echo "<pre>";
                print_r($mainmenudetails);
                echo "</pre>";*/
                $data['mainmenudetails']=$mainmenudetails[0];
            }            
        }
        
        $data['customlinks_type_arr']=  $this->mainmenu_model->get_headermenulinks($customlinks_page_id);        
        $data['partnerlang']=$partnerlang;
        
        $this->load->library('breadcrumb');
        $this->breadcrumb->seperator='<div class="breadcrumb_divider"></div>';
        $this->breadcrumb->addCrumb('Mainmenu',base_url()."secu/mainmenu/");
        $this->breadcrumb->addCrumb('Manage Mainmenu',"#");
        $data['breadcrumb'] = $this->breadcrumb->makeBread();          
        
        $this->template->set('controller', $this);
        $this->template->set('title', 'mainmenu');
        $this->template->load_partial('admintemplate', 'mainmenu/mainmenu', $data);
    }
    
    /*checks whether anny data exist for a particular customlinks page type*/
    public function loadcustompagewisedetails(){
        $this->load->model("mainmenu_model");
        $tmp_customlinks_id='';
        $customlinks_id=$this->uri->segment(5);
        $mainmenu_id = $this->mainmenu_model->get_custom_list_id($customlinks_id);
        if ($mainmenu_id[0]['mainmenu_id']==0){
            $tmp_customlinks_id=$customlinks_id;
        }
        redirect(base_url() . 'secu/mainmenu/managemainmenu/id/'.$mainmenu_id[0]['mainmenu_id'].'/'.$tmp_customlinks_id);
    }
    
    public function doaddmainmenu()
    {
        
        $partnerlang = $this->config->item("partnerlang");
        $this->load->model("mainmenu_model");
        $validate_arr_lang=array();
        $mainmenu_detail_arr_lang=array();
        foreach($partnerlang as $langkey=>$langval){
            $validate_arr_lang+=array(array( 'field' => 'title'.$langkey,'label' => 'Title-'.$langkey,'rules' => 'required'),
                                    array( 'field' => 'shortdesc'.$langkey,'label' => 'Short Description-'.$langkey,'rules' => 'required'),
                                    array( 'field' => 'desc'.$langkey,'label' => 'Description-'.$langkey,'rules' => 'required')
                );
            $mainmenu_detail_arr_lang+=array("title".$langkey=>$this->input->post('title'.$langkey, TRUE),
                                        "shortdesc".$langkey=>$this->input->post('shortdesc'.$langkey, TRUE),
                                        "desc".$langkey=>$this->input->post('desc'.$langkey, TRUE));
        }
        //print_r($mainmenu_detail_arr_lang);
        //exit();
        $validate_mainmenu = array_merge($validate_arr_lang, array( 
            array( 'field' => 'customlinks_id',
                    'label' => 'customlinks Page',
                    'rules' => 'required')
        ));

        $mainmenudetails = array_merge($mainmenu_detail_arr_lang, array(
        "customlinks_id"  =>$this->input->post('customlinks_id'),        
        "status"  =>$this->input->post('status')
            ));
        if ($this->input->post('id')){
            $mainmenudetails=  array_merge(array("mainmenu_id"=>$this->input->post('id')), $mainmenudetails);
        }
        //$hotel_arr = $this->mainmenu_model->get_hotel_list();
        //print_r($hotel_arr);
        //$data['hotel_arr']=$hotel_arr;
        
        //$this->session->set_userdata($mainmenudetails);
        //exit('fgdf');
        $this->form_validation->set_error_delimiters('<div class="alert">', '</div>');
        $this->form_validation->set_rules($validate_mainmenu);
        
        $data['partnerlang']=$partnerlang;
        $data['mainmenudetails']=$mainmenudetails;
        //print_r($this->form_validation->run());exit;
            

        if ($this->form_validation->run() == FALSE) {
            //exit('dee');
            //print_r($mainmenudetails);
    //exit();
            //$this->template->load('admintemplate', 'admin/hotel',$data);
            $this->template->set('controller', $this);
            $this->template->set('title', "login");
            $this->template->load_partial('admintemplate', 'mainmenu/mainmenu',$data);
        } else  {
            //$this->template->set('product_header', 'Admin Home');
            $this->load->model('mainmenu_model');
            
            if ($this->input->post('id')){
            
                $this->mainmenu_model->updatemainmenu($mainmenudetails, $data);
                $mainmenu_id=$this->input->post('id');
                //exit('yyy');
            }else{                
                $mainmenu_id=$this->mainmenu_model->savemainmenu($mainmenudetails, $partnerlang);
            }            
            if($mainmenu_id>0){
                redirect(base_url() . 'secu/mainmenu/managemainmenu/id/'.$mainmenu_id);
            }
            redirect(base_url() . 'secu/mainmenu/');
        }


    }

    public function delmainmenu()
    {
        $mainmenu_id = $this->input->post("mainmenu_id");
        $this->load->model('mainmenu_model');
        $this->mainmenu_model->delete_mainmenu($mainmenu_id);
        redirect(base_url() . 'secu/mainmenu/customlinkslist');
    }

        public function savemainmenuimages()
     {
        $upload_from= $this->uri->segment(4);
        //exit($upload_from);
        $id=$this->input->post("id");
        $c_upload['upload_path']    = $this->imgdata['dir']['original'];
        $c_upload['allowed_types']  = 'gif|jpg|png|jpeg|x-png';
        $c_upload['max_size']       = '10000';
        $c_upload['max_width']      = '10000';
        $c_upload['max_height']     = '5000';
        $c_upload['remove_spaces']  = TRUE;
                
        $this->load->library('upload', $c_upload);

        if ($this->upload->do_upload()) {
//
            $img = $this->upload->data();
            $new_image = $this->imgdata['dir']['original'].trim($id)."_".trim(time())."_".$img['file_name'];
            $this->img_resize($img, $new_image,$id);
            
            unlink($this->imgdata['dir']['original'].$img['file_name']);
            //exit($this->imgdata['dir']['original'].$img['file_name']);
            $this->load->model('content');
            //exit();
            $this->content->saveimagestodb($id,trim($id)."_".trim(time())."_".$img['file_name'],$upload_from);



        } else {
            $this->imgerror= $this->upload->display_errors();            
        }
        if($upload_from=='mainmenuimg'){
            redirect('secu/mainmenu/managemainmenu/id/'.$id);
        }else{
            redirect('admin/hotelcontent/?id='.trim($id)."#img");
        }

    }
    
    public function img_resize($upload,$new_image,$id)
    {
        $this->load->library('image_lib');
        $newpath = $new_image;

        $config['image_library'] = 'gd2';
        $config['source_image']    = $upload['full_path'];
        $config['new_image'] = $newpath;
        $config['maintain_ratio'] = TRUE;
        $config['width']     = 595;
        $config['height']    = 320;

        $this->image_lib->initialize($config);

        if ( ! $this->image_lib->resize())
        {
            echo $this->image_lib->display_errors();
        }

        $newpath_thumb = $this->imgdata['dir']['thumb']."thumb_".trim($id)."_".trim(time())."_".$upload['file_name'];

        unset($config);
        $this->image_lib->clear();
         $this->make_thumb($upload['full_path'],$newpath_thumb);
    }
    
    public function make_thumb($tempimage,$path)
    {

        $config['image_library'] = 'gd2';
        $config['source_image']    = $tempimage;
        $config['new_image'] = $path;
        $config['maintain_ratio'] = TRUE;
        $config['width']     = 100;
        $config['height']    = 80;

        $this->image_lib->initialize($config);

        if ( ! $this->image_lib->resize())
        {
            echo $this->image_lib->display_errors();
        }
    }
    
    public function deletemainmenuimage()
    {
        $original_status=false;
        $thumb_status=false;
        $del_image_status="failed";
        $id=$this->uri->segment(7);
        $ori_img=$this->uri->segment(5); 
        $ext=  substr($ori_img, -3);        
        $ori_img=  substr($ori_img, 0, strlen($ori_img)-4).'.'.$ext;
        $this->load->model('content');
        $this->content->deleteimage($id, "mainmenuimg");
        //
        //echo $this->imgdata['dir']['original'].$ori_img.'<br>';
        if (file_exists($this->imgdata['dir']['original'].$ori_img)){
            unlink($this->imgdata['dir']['original'].$ori_img);
            $original_status=true;
        }
        //echo $this->imgdata['dir']['thumb'].'thumb_'.$ori_img;
        if (file_exists($this->imgdata['dir']['thumb'].'thumb_'.$ori_img)){
            unlink($this->imgdata['dir']['thumb'].'thumb_'.$ori_img);
            $thumb_status=true;
        }
        //exit();
        if ($original_status && $thumb_status){
            $del_image_status="successfully";
        }elseif ($original_status || $thumb_status) {
            $del_image_status="partially";
        }
        redirect('secu/mainmenu/managemainmenu/id/'.$id.'/'.$ori_img.'/'.$del_image_status);
    }
}
?>